﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;

namespace BasePlatformCommon
{
	public class LanguageOper
	{
		#region DllImport

		[DllImport("kernel32")]
		private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

		[DllImport("kernel32")]
		private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

		#endregion

		public static LanguageEnum LocalLanguage
		{
			get
			{
				return _LocalLanguage;
			}
			set
			{ 
				_LocalLanguage = value;
				
				if (value == LanguageEnum.CHS)
				{
					iniFilePath = GetAppPath() + @"\agent-ini\agent_chs.ini";
				}
				else if (value == LanguageEnum.EN)
				{
					iniFilePath = GetAppPath() + @"\agent-ini\agent_en.ini";
				}
				else if (value == LanguageEnum.CHT)
				{
					iniFilePath = GetAppPath() + @"\agent-ini\agent_cht.ini";
				}

				SetIniFilePath(iniFilePath);
			}
		}

		public static bool available = false;

		private static LanguageEnum _LocalLanguage = LanguageEnum.CHS;

		private static string iniFilePath;

		private static Dictionary<string, string> stringDictionary = new Dictionary<string, string>();

		public static string GetAppPath()
		{
			string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
			return path.Substring(0, path.LastIndexOf(@"\"));
		}

		// 设置 ini 文件路径, 读取键值对
		public static void SetIniFilePath(string path)
		{
			if (path == null || path.Trim().Equals(""))
			{
				available = false;
				return;
			}
			else if (!File.Exists(path))
			{
				available = false;
				return;
			}

			string debugStr = "";
			try
			{
				iniFilePath = path;
				stringDictionary.Clear();
				FileStream fs = new FileStream(iniFilePath, FileMode.Open, FileAccess.Read);
				StreamReader reader = new StreamReader(fs, Encoding.Unicode);
				string currentLine = "";
				string section = "";
				string key = "";
				string value = "";
				while (true)
				{
					currentLine = reader.ReadLine();
					if (currentLine == null)
					{
						break;
					}

					currentLine = currentLine.Trim();

					if (currentLine.Equals(""))
					{
						continue;
					}
					else if (currentLine.StartsWith(";"))
					{
						continue;
					}
					else
					{
						if (currentLine.StartsWith("[") && currentLine.EndsWith("]"))
						{
							section = currentLine.Substring(1, currentLine.Length - 2);
							continue;
						}
						else
						{
							string[] subStr = currentLine.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
							key = subStr[0].Trim();
							value = subStr[1].Trim();
							debugStr = (section + "_" + key);
							stringDictionary.Add(section + "_" + key, value);
						}
					}
				}

				available = true;
			}
			catch (Exception ex)
			{
				Log.NormalLogger.Info("Language ini File Parse Error.\n" + ex.Message + ex.StackTrace + "debugStr = " + debugStr);
				stringDictionary.Clear();
				available = false;
			}
		}

		// 读取本地化值
		public static string ReadValue(string section, string key)
		{
			string innerKey = section + "_" + key;
			string value = "";
			if (stringDictionary.ContainsKey(innerKey))
			{
				if (stringDictionary.TryGetValue(innerKey, out value) == true)
				{
					return value;
				}
				else
				{
					return "";
				}
			}
			else
			{
				return "";
			}
		}

		// 设置当前语言文化
		public static void SetCurrentCulture()
		{
			if (LanguageOper.LocalLanguage == LanguageEnum.CHS)
			{
				Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-CHS");
				Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CHS");
				MessageBoxManager.Unregister();
				MessageBoxManager.OK = "确定";
				MessageBoxManager.Yes = "是";
				MessageBoxManager.No = "否";
				MessageBoxManager.Cancel = "取消";
				MessageBoxManager.Retry = "重试";
				MessageBoxManager.Ignore = "忽略";
				MessageBoxManager.Abort = "关于";
				MessageBoxManager.Register();
			}
			else if (LanguageOper.LocalLanguage == LanguageEnum.CHT)
			{
				Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-CHT");
				Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CHT");
				MessageBoxManager.Unregister();
				MessageBoxManager.OK = "確定";
				MessageBoxManager.Yes = "是";
				MessageBoxManager.No = "否";
				MessageBoxManager.Cancel = "取消";
				MessageBoxManager.Retry = "重試";
				MessageBoxManager.Ignore = "忽略";
				MessageBoxManager.Abort = "關於";
				MessageBoxManager.Register();
			}
			else if (LanguageOper.LocalLanguage == LanguageEnum.EN)
			{
				Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
				Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
				MessageBoxManager.Unregister();
				MessageBoxManager.OK = "OK";
				MessageBoxManager.Yes = "Yes";
				MessageBoxManager.No = "No";
				MessageBoxManager.Cancel = "Cancel";
				MessageBoxManager.Retry = "Retry";
				MessageBoxManager.Ignore = "Ignore";
				MessageBoxManager.Abort = "About";
				MessageBoxManager.Register();
			}
		}
	}

	public enum LanguageEnum
	{
 		CHS = 0,
		EN = 1,
		CHT = 2
	}
}
